package com.lonwin.inspectionmanagement.app.utils.Listener;

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.lh.LhEarthSurfaceView;
import com.lh.LhSmoothEarthSurfaceView;
import com.lh.event.LhInputHandler;
import com.lh.features.LhCoordinate;
import com.lh.features.renderable.LhPointPlacemark;
import com.lonwin.inspectionmanagement.mvp.ui.activity.HomeActivity;

import java.util.Observable;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

//龙慧地球事件内部类
public class LhEarthListener extends GestureDetector.SimpleOnGestureListener implements LhInputHandler
{
    protected static final int SINGLE_TAP_INTERVAL = 300;
    protected long mLastTap = -1;
    private GestureDetector gestureScanner;
    protected boolean isDraggingArmed=false;
    protected LhPointPlacemark selectedPlacemark=null;
    protected LhPointPlacemark pickedPlacemark=null;
    protected ScreenMoveListener  mscreenMoveListener;
    LhEarthSurfaceView homeLhEarthSurfaceView;
    private long time=0;
    private Disposable disposable=null;
    public LhEarthListener(ScreenMoveListener  screenMoveListener, LhEarthSurfaceView lhEarthSurfaceView)
    {
        mscreenMoveListener=screenMoveListener;
        homeLhEarthSurfaceView=lhEarthSurfaceView;
        gestureScanner = new GestureDetector(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO Auto-generated method stub
        gestureScanner.onTouchEvent(event);

        if (event.getAction()==MotionEvent.ACTION_MOVE)
        {

//            if (disposable!=null&&!disposable.isDisposed())
//            {
//                disposable.dispose();
//            }
            disposable=   io.reactivex.Observable.just(homeLhEarthSurfaceView).delay(1,TimeUnit.SECONDS).subscribe(new Consumer<LhEarthSurfaceView>() {
                @Override
                public void accept(LhEarthSurfaceView mhomeLhEarthSurfaceView) {
                    mhomeLhEarthSurfaceView.setCrossHairsPoint();
                    LhCoordinate coord = mhomeLhEarthSurfaceView.getCrossHairsCoordinate();
                    Log.i("####", event.getAction() + "");
                    if (coord != null) {
                        //获取屏幕经纬度   coord.getLatitude()
                        mscreenMoveListener.onMoveListener(coord);
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    Log.i("##",throwable.getMessage());
                }
            });
        }

//        if (event.getAction()==MotionEvent.ACTION_MOVE&&(System.currentTimeMillis()-time)/1000>1)
//        {
//            homeLhEarthSurfaceView.setCrossHairsPoint();
//            LhCoordinate coord = homeLhEarthSurfaceView.getCrossHairsCoordinate();
//            Log.i("####",event.getAction()+"");
//            if(coord!=null)
//            {
//                //获取屏幕经纬度   coord.getLatitude()
//                mscreenMoveListener.onMoveListener(coord);
//            }
//            time=System.currentTimeMillis();
//        }
        return false;
    }



    public interface ScreenMoveListener{
        void onMoveListener(LhCoordinate lhCoordinate);
    }
}
